package com.yinsin.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.yinsin.other.LogHelper;

public class ResourceUtils {
	private static final LogHelper logger = LogHelper.getLogger(ResourceUtils.class);

	public static String readResourceFile(String file) {
		String content = "";
		try {
			InputStream is = ResourceUtils.class.getClassLoader().getResourceAsStream(file);
			byte[] b = null;
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = is.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			content = new String(b, "utf-8");
			baos.flush();
			baos.close();
			is.close();
		} catch (Exception e) {
			logger.error("读取资源文件异常：" + e.getMessage());
		}
		return content;
	}

	public static byte[] readResource(String file) {
		byte[] b = null;
		try {
			InputStream is = ResourceUtils.class.getClassLoader().getResourceAsStream(file);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = is.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			is.close();
		} catch (Exception e) {
			logger.error("读取资源文件异常：" + e.getMessage());
		}
		return b;
	}

}
